TransformSystem = System.new()

function TransformSystem:update(dt)
    if self.paused and not self.updateOnce then
        return
    end
    if self.updateOnce then
        Console.log(".................")
    end
    -- 先更新尺寸，后更新位置，处理layout/stretch/align等复杂的关系
    self:updateSize(Node.Root)
    self:updatePositions(Node.Root)
    -- 更新拖拽组件限制范围
    self:updateDragLimit(Node.Root)
    -- 根据拖拽组件限制范围更新对象的坐标
    self:updateDragItemPos(Node.Root)
    if self.updateOnce then
        self.updateOnce = false
    end
end

function TransformSystem:updateSize(node)
    if node.stretchDirty then
        node:updateStretch()
    end
    if node.layoutSizeDirty then
        for i, v in ipairs(node.children) do
            self:updateSize(v)
        end
        node:updateLayoutSize()
        return
    end
    for i, v in ipairs(node.children) do
        self:updateSize(v)
    end
end

function TransformSystem:updatePositions(node)
    if node.layoutDirty then
        node:updateLayout()
    end
    if node.alignDirty then
        node:updateAlign()
    end
    for i, v in ipairs(node.children) do
        self:updatePositions(v)
    end
end

function TransformSystem:updateDragLimit(node)
    if node.dragLimitDirty then
        node:_updateDragLimit()
    end
    for i, v in ipairs(node.children) do
        self:updateDragLimit(v)
    end
end

function TransformSystem:updateDragItemPos(node)
   if node.dragLimitChanged then
        node:upatePositionByDragLimit(node.x, node.y)
        node.dragLimitChanged = false
   end
   for i, v in ipairs(node.children) do
       self:updateDragItemPos(v)
   end
end

RegisterSystem(TransformSystem)